require 'singleton'

class Semaphor::ReportRegistry
  include Singleton
  
  instance.extend Enumerable
  
  @@reports = Hash.new []
  
  def self.register_directory(dir, root)
    Dir[File.join(root,dir,"**","*.rb")].each do |path|
      lib = path.gsub("#{root}/", "").gsub(".rb", "")
      puts "loading lib #{lib}"
      require lib
      report_class = lib.camelize.constantize
      puts "class is #{report_class}"
      unless report_class.name.nil?
        puts "adding #{report_class} to registry"
        self[report_class.category] <<= report_class.to_s
      end
    end
  end
  
  def each(&block)
    @@reports.each(&block)
  end
  
  def self.method_missing(method, *args, &block)
    self.instance.send(method, *args, &block)
  end
  
  def method_missing(method, *args, &block)
    @@reports.send(method, *args, &block)
  end
end